//
//  CarDetail.m
//  
//
//  Created by 微 醺 on 15/10/6.
//
//

#import "CarDetail.h"
#import "NetworkHandle.h"
#import "CarDetailHeaderView.h"
#import "CarDetailTableViewCell.h"
#import "SearchModel.h"

@interface CarDetail () <UITableViewDataSource , UITableViewDelegate>

@property (nonatomic , strong)UITableView *tableView;
@property (nonatomic , strong)UIImageView *carPicView;
@property (nonatomic , strong)UILabel *carNameLabel;
@property (nonatomic , strong)UILabel *carPriceLabel;
@property (nonatomic , strong)UIImageView *lineView;
@property (nonatomic , strong)UIImageView *lineViewSec;
@property (nonatomic , strong)UILabel *carlevelName;
@property (nonatomic , strong)NSDictionary *contentDic;
@property (nonatomic , strong)NSMutableArray *contentArr;
@property (nonatomic , strong)UIView *tempView;

@end

@implementation CarDetail
- (void)viewWillAppear:(BOOL)animated
{
    AppDelegate *dele = [[UIApplication sharedApplication]delegate];
    if (dele.isNight == 1) {
        self.view.backgroundColor = [UIColor blackColor];
        self.tableView.backgroundColor = [UIColor blackColor];
        self.carNameLabel.textColor = [UIColor whiteColor];
        [self.tableView reloadData];
    }
    else{
        self.view.backgroundColor = [UIColor whiteColor];
        self.tableView.backgroundColor = [UIColor whiteColor];
        self.carNameLabel.textColor = [UIColor blackColor];
        [self.tableView reloadData];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.hidden = NO;
    self.title = @"车辆信息";
    NSLog(@"%f" , self.navigationController.navigationBar.frame.size.height);
    self.navigationController.automaticallyAdjustsScrollViewInsets = NO;
    
    [self dataHandle];
    [self subViews];
    
}

- (void)dataHandle
{
    NSString *carBasicURL = [NSString stringWithFormat:@"http://mi.xcar.com.cn/interface/xcarapp/getSeriesInfoNew.php?seriesId=%ld&uid=" , self.seriesId];
    
    [NetworkHandle GETWithURL:carBasicURL complication:^(id result) {

        NSDictionary *dic = result;
        self.carNameLabel.text = [NSString stringWithFormat:@"    %@" , [dic objectForKey:@"seriesName"]];
        self.carPriceLabel.text =[NSString stringWithFormat:@"    指导价:%@" , [dic objectForKey:@"price"]];
        self.carlevelName.text = [dic objectForKey:@"levelName"];
        [self.carPicView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"image"]] placeholderImage:[UIImage imageNamed:@"placeHolder"]];
        
    }];
    
    NSString *carConfigURL = [NSString stringWithFormat:@"http://mi.xcar.com.cn/interface/xcarapp/getCarParametersById.php?seriesId=%ld&carId=&ver=6.1.6" , self.seriesId];
    
    [NetworkHandle GETWithURL:carConfigURL complication:^(id result) {
       
        self.contentDic = result;
        self.contentArr = [self.contentDic objectForKey:@"config"];
        [self tableViewSubView];
        
    }];

}

#pragma mark - 汽车图片等基本信息
- (void) subViews
{
    /*详细信息占位图*/
    self.tempView = [[UIView alloc]initWithFrame:CGRectMake(0, 396.0/667*self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - 396.0/667*self.view.frame.size.height)];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SAWindth * 200, SAHeight *  50)];
    label.center = self.tempView.center;
    label.text = @"暂无车辆信息";
    label.textAlignment = 1;
    
    /*汽车图片*/
    self.carPicView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 64.0/667*self.view.frame.size.height, self.view.frame.size.width, 250.0/667*self.view.frame.size.height)];
    
    /*汽车名*/
    self.carNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 314.0/667*self.view.frame.size.height, self.view.frame.size.width/2, 40.0/667*self.view.frame.size.height)];
    self.carNameLabel.font = [UIFont systemFontOfSize:19];
    
    /*汽车类型*/
    self.carlevelName = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2, 314.0/667*self.view.frame.size.height, self.view.frame.size.width/2, 40.0/667*self.view.frame.size.height)];
    self.carlevelName.textAlignment = 1;
    self.carlevelName.textColor = [UIColor grayColor];
    self.carlevelName.font = [UIFont systemFontOfSize:16];
    
    /*分割线*/
    self.lineView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 354.0/667*self.view.frame.size.height, self.view.frame.size.width, SAHeight *  1)];
    self.lineView.backgroundColor = [UIColor colorWithRed:244.0f/255 green:244.0f/255  blue:244.0f/255  alpha:1];
    
    /*汽车价格*/
    self.carPriceLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 355.0/667*self.view.frame.size.height, self.view.frame.size.width, 40.0/667*self.view.frame.size.height)];
    self.carPriceLabel.textColor = [UIColor orangeColor];
    self.carPriceLabel.font = [UIFont systemFontOfSize:17];
    
    /*分割线2*/
    self.lineViewSec = [[UIImageView alloc]initWithFrame:CGRectMake(0, 395.0/667*self.view.frame.size.height, self.view.frame.size.width, SAHeight *  1)];
    self.lineViewSec.backgroundColor = [UIColor colorWithRed:244.0f/255 green:244.0f/255  blue:244.0f/255  alpha:1];
    
    [self.view addSubview:self.carPicView];
    [self.view addSubview:self.carNameLabel];
    [self.view addSubview:self.carPriceLabel];
    [self.view addSubview:self.lineViewSec];
    [self.view addSubview:self.lineView];
    [self.view addSubview:self.carlevelName];
    [self.view addSubview:self.tempView];
    [self.view addSubview:label];
    

}


#pragma mark - 汽车参数tableView
- (void) tableViewSubView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 396.0/667*self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - 396.0/667*self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.backgroundColor = NULL;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = 0;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.bounces = NO;
    [self.view addSubview:self.tableView];
    
    [self.tableView registerClass:[CarDetailTableViewCell class] forCellReuseIdentifier:@"reuse"];

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return SAHeight *  30;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return SAHeight *  50;
}

#pragma mark - 分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.contentArr.count;
}



#pragma mark - 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *arr = [self.contentArr[section] objectForKey:@"result"];
    return arr.count;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    CarDetailHeaderView *headerView = [[CarDetailHeaderView alloc]initWithReuseIdentifier:@"headerReuse"];
    headerView.backgroundColor = [UIColor lightGrayColor];
    
    headerView.headerTitle.text = [self.contentArr[section] objectForKey:@"typename"];
    
    return headerView;
    
}

#pragma mark - cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CarDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    
    NSArray *arr = [self.contentArr[indexPath.section] objectForKey:@"result"];
    SearchModel *model = [[SearchModel alloc]init];
    [model setValuesForKeysWithDictionary:arr[indexPath.row]];
    cell.model = model;
    cell.selectionStyle = 0;
    
    return cell;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
